#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2023/4/11 15:24
@File: postgresql.py
"""
import jaydebeapi
import psycopg2
from .basic import DatabaseUtil


class PostgreSqlUtil(DatabaseUtil):

    def __init__(self, user, password, ip, port=11345, **kwargs):
        self.CURSOR = {}
        # server = kwargs['server'] if 'server' in kwargs else user
        # conn_string = f"dbname={server} user={user} host={ip} port={port} password={password}"
        # self.db = psycopg2.connect(conn_string)
        driver = 'org.postgresql.Driver'
        server_name = kwargs.get('server', 'postgres')
        schema_name = kwargs.get('schema', '')
        url = f'jdbc:postgresql://{ip}:{port}/{server_name}'
        if schema_name:
            url += f"?currentSchema={schema_name}"
        jar_ile = self.getJarFile()
        self.db = jaydebeapi.connect(driver, url, [user, password], jar_ile)
        self.db.jconn.setAutoCommit(False)


if __name__ == '__main__':
    with PostgreSqlUtil('hspbs','hspbs','10.20.26.62',server='postgres') as db:
        r=db.execsql(f"select * from pg_database")
        print(r)